using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.Serialization;
using CodeAngel.Domain;
using CodeAngel.Domain.Framework;

namespace CodeAngel.Services
{
    /// <summary>
    /// A collection of domain-specific extension methods used to extend DTO funtionality.
    /// </summary>
	public static class DtoExtensions
	{

		#region CreateSaveParameters

        /// <summary>
        /// Creates the parameters used for a save operation for the identified DTO, and adds them
        /// to the identified parameters collection.
        /// </summary>
		public static void CreateSaveParameters(this IDto thisDto, SqlParameterCollection parameters)
		{
            if (thisDto is Role_PermissionDto)
            {
                var thisRole_PermissionDto = thisDto as Role_PermissionDto;
                parameters.Add("@RoleID", SqlDbType.UniqueIdentifier, 50, "RoleID").Value = thisRole_PermissionDto.RoleID;
                parameters.Add("@PermissionID", SqlDbType.UniqueIdentifier, 50, "PermissionID").Value = thisRole_PermissionDto.PermissionID;
                parameters.Add("@Role_PermissionID", SqlDbType.UniqueIdentifier, 50, "Role_PermissionID").Value = thisRole_PermissionDto.Role_PermissionID;
                var TimeStampParameter = parameters.Add("@TimeStamp", SqlDbType.Timestamp, -1, "TimeStamp");
                TimeStampParameter.Value = thisRole_PermissionDto.TimeStamp;
                TimeStampParameter.Direction = thisDto.State == DtoChangedState.Modified ? ParameterDirection.InputOutput : ParameterDirection.Output;
            }
            else if (thisDto is EntityDefDto)
            {
                var thisEntityDefDto = thisDto as EntityDefDto;
                parameters.Add("@TableName", SqlDbType.VarChar, 50, "TableName").Value = thisEntityDefDto.TableName;
                parameters.Add("@Notes", SqlDbType.VarChar, 50, "Notes").Value = thisEntityDefDto.Purpose;
                parameters.Add("@FriendlyName", SqlDbType.VarChar, 50, "FriendlyName").Value = thisEntityDefDto.FriendlyName;
                parameters.Add("@FilterRelationID", SqlDbType.UniqueIdentifier, 50, "FilterRelationID").Value = thisEntityDefDto.FilterRelationID;
                parameters.Add("@IsNameKeyword", SqlDbType.Bit, 50, "IsNameKeyword").Value = thisEntityDefDto.IsNameKeyword;
                parameters.Add("@Name", SqlDbType.VarChar, 50, "Name").Value = thisEntityDefDto.Name;
                parameters.Add("@SubFolder", SqlDbType.VarChar, 50, "SubFolder").Value = thisEntityDefDto.SubFolder;
                parameters.Add("@CanBeAggregateRoot", SqlDbType.Bit, 50, "CanBeAggregateRoot").Value = thisEntityDefDto.CanBeAggregateRoot;
                parameters.Add("@FriendlyID", SqlDbType.VarChar, 8, "FriendlyID").Value = thisEntityDefDto.FriendlyID;
                parameters.Add("@EntityDefID", SqlDbType.UniqueIdentifier, 50, "EntityDefID").Value = thisEntityDefDto.EntityDefID;
                parameters.Add("@DomainID", SqlDbType.UniqueIdentifier, 50, "DomainID").Value = thisEntityDefDto.DomainID;
                var TimeStampParameter = parameters.Add("@TimeStamp", SqlDbType.Timestamp, -1, "TimeStamp");
                TimeStampParameter.Value = thisEntityDefDto.TimeStamp;
                TimeStampParameter.Direction = thisDto.State == DtoChangedState.Modified ? ParameterDirection.InputOutput : ParameterDirection.Output;
            }
            else if (thisDto is DomainDto)
            {
                var thisDomainDto = thisDto as DomainDto;
                parameters.Add("@Namespace", SqlDbType.VarChar, 50, "Namespace").Value = thisDomainDto.Namespace;
                parameters.Add("@ProjectName", SqlDbType.VarChar, 50, "ProjectName").Value = thisDomainDto.ProjectName;
                parameters.Add("@ProductionDatabaseID", SqlDbType.UniqueIdentifier, 50, "ProductionDatabaseID").Value = thisDomainDto.ProductionDatabaseID;
                parameters.Add("@Notes", SqlDbType.VarChar, 50, "Notes").Value = thisDomainDto.Notes;
                parameters.Add("@FriendlyID", SqlDbType.VarChar, 8, "FriendlyID").Value = thisDomainDto.FriendlyID;
                parameters.Add("@DevelopmentDatabaseID", SqlDbType.UniqueIdentifier, 50, "DevelopmentDatabaseID").Value = thisDomainDto.DevelopmentDatabaseID;
                parameters.Add("@ProjectRelativePath", SqlDbType.VarChar, 50, "ProjectRelativePath").Value = thisDomainDto.ProjectRelativePath;
                parameters.Add("@DomainID", SqlDbType.UniqueIdentifier, 50, "DomainID").Value = thisDomainDto.DomainID;
                parameters.Add("@Name", SqlDbType.VarChar, 50, "Name").Value = thisDomainDto.Name;
                parameters.Add("@ServicesProjectRelativePath", SqlDbType.VarChar, 50, "ServicesProjectRelativePath").Value = thisDomainDto.ServicesProjectRelativePath;
                parameters.Add("@ServicesProjectName", SqlDbType.VarChar, 50, "ServicesProjectName").Value = thisDomainDto.ServicesProjectName;
                var TimeStampParameter = parameters.Add("@TimeStamp", SqlDbType.Timestamp, -1, "TimeStamp");
                TimeStampParameter.Value = thisDomainDto.TimeStamp;
                TimeStampParameter.Direction = thisDto.State == DtoChangedState.Modified ? ParameterDirection.InputOutput : ParameterDirection.Output;
            }
            else if (thisDto is FilterRelationDto)
            {
                var thisFilterRelationDto = thisDto as FilterRelationDto;
                parameters.Add("@EntityDefID", SqlDbType.UniqueIdentifier, 50, "EntityDefID").Value = thisFilterRelationDto.EntityDefID;
                parameters.Add("@PropertyDefID", SqlDbType.UniqueIdentifier, 50, "PropertyDefID").Value = thisFilterRelationDto.PropertyDefID;
                parameters.Add("@ParentFilterRelationID", SqlDbType.UniqueIdentifier, 50, "ParentFilterRelationID").Value = thisFilterRelationDto.ParentFilterRelationID;
                parameters.Add("@FriendlyID", SqlDbType.VarChar, 8, "FriendlyID").Value = thisFilterRelationDto.FriendlyID;
                parameters.Add("@FilterRelationID", SqlDbType.UniqueIdentifier, 50, "FilterRelationID").Value = thisFilterRelationDto.FilterRelationID;
                var TimeStampParameter = parameters.Add("@TimeStamp", SqlDbType.Timestamp, -1, "TimeStamp");
                TimeStampParameter.Value = thisFilterRelationDto.TimeStamp;
                TimeStampParameter.Direction = thisDto.State == DtoChangedState.Modified ? ParameterDirection.InputOutput : ParameterDirection.Output;
            }
            else if (thisDto is PropertyDefDto)
            {
                var thisPropertyDefDto = thisDto as PropertyDefDto;
                parameters.Add("@IsNameKeyword", SqlDbType.Bit, 50, "IsNameKeyword").Value = thisPropertyDefDto.IsNameKeyword;
                parameters.Add("@IsPrimaryKey", SqlDbType.Bit, 50, "IsPrimaryKey").Value = thisPropertyDefDto.IsPrimaryKey;
                parameters.Add("@Notes", SqlDbType.VarChar, 50, "Notes").Value = thisPropertyDefDto.Purpose;
                parameters.Add("@SqlDataPrecision", SqlDbType.Int, 50, "SqlDataPrecision").Value = thisPropertyDefDto.SqlDataPrecision;
                parameters.Add("@EntityDefID", SqlDbType.UniqueIdentifier, 50, "EntityDefID").Value = thisPropertyDefDto.EntityDefID;
                parameters.Add("@SqlDataScale", SqlDbType.Int, 50, "SqlDataScale").Value = thisPropertyDefDto.SqlDataScale;
                parameters.Add("@PropertyDefID", SqlDbType.UniqueIdentifier, 50, "PropertyDefID").Value = thisPropertyDefDto.PropertyDefID;
                parameters.Add("@SqlDefaultValue", SqlDbType.VarChar, 50, "SqlDefaultValue").Value = thisPropertyDefDto.SqlDefaultValue;
                parameters.Add("@DefaultValue", SqlDbType.VarChar, 50, "DefaultValue").Value = thisPropertyDefDto.DefaultValue;
                parameters.Add("@DatabaseColumnName", SqlDbType.VarChar, 50, "DatabaseColumnName").Value = thisPropertyDefDto.DatabaseColumnName;
                parameters.Add("@ForeignKeyPropertyDefID", SqlDbType.UniqueIdentifier, 50, "ForeignKeyPropertyDefID").Value = thisPropertyDefDto.ForeignKeyPropertyDefID;
                parameters.Add("@Name", SqlDbType.VarChar, 50, "Name").Value = thisPropertyDefDto.Name;
                parameters.Add("@FriendlyName", SqlDbType.VarChar, 50, "FriendlyName").Value = thisPropertyDefDto.FriendlyName;
                parameters.Add("@IsFriendlyID", SqlDbType.Bit, 50, "IsFriendlyID").Value = thisPropertyDefDto.IsFriendlyID;
                parameters.Add("@SqlDataType", SqlDbType.VarChar, 50, "SqlDataType").Value = thisPropertyDefDto.SqlDataType;
                parameters.Add("@DataType", SqlDbType.VarChar, 50, "DataType").Value = thisPropertyDefDto.DataType;
                parameters.Add("@IsNullable", SqlDbType.Bit, 50, "IsNullable").Value = thisPropertyDefDto.IsNullable;
                parameters.Add("@FriendlyID", SqlDbType.VarChar, 8, "FriendlyID").Value = thisPropertyDefDto.FriendlyID;
                parameters.Add("@SqlIsIdentity", SqlDbType.Bit, 50, "SqlIsIdentity").Value = thisPropertyDefDto.SqlIsIdentity;
                parameters.Add("@SqlDataLength", SqlDbType.Int, 50, "SqlDataLength").Value = thisPropertyDefDto.SqlDataLength;
                var TimeStampParameter = parameters.Add("@TimeStamp", SqlDbType.Timestamp, -1, "TimeStamp");
                TimeStampParameter.Value = thisPropertyDefDto.TimeStamp;
                TimeStampParameter.Direction = thisDto.State == DtoChangedState.Modified ? ParameterDirection.InputOutput : ParameterDirection.Output;
            }
            else if (thisDto is User_PermissionDto)
            {
                var thisUser_PermissionDto = thisDto as User_PermissionDto;
                parameters.Add("@PermissionID", SqlDbType.UniqueIdentifier, 50, "PermissionID").Value = thisUser_PermissionDto.PermissionID;
                parameters.Add("@User_PermissionID", SqlDbType.UniqueIdentifier, 50, "User_PermissionID").Value = thisUser_PermissionDto.User_PermissionID;
                parameters.Add("@UserID", SqlDbType.UniqueIdentifier, 50, "UserID").Value = thisUser_PermissionDto.UserID;
                var TimeStampParameter = parameters.Add("@TimeStamp", SqlDbType.Timestamp, -1, "TimeStamp");
                TimeStampParameter.Value = thisUser_PermissionDto.TimeStamp;
                TimeStampParameter.Direction = thisDto.State == DtoChangedState.Modified ? ParameterDirection.InputOutput : ParameterDirection.Output;
            }
            else if (thisDto is PermissionDto)
            {
                var thisPermissionDto = thisDto as PermissionDto;
                parameters.Add("@OperationType", SqlDbType.VarChar, 50, "OperationType").Value = thisPermissionDto.OperationType;
                parameters.Add("@Name", SqlDbType.VarChar, 50, "Name").Value = thisPermissionDto.Name;
                parameters.Add("@ObjectName", SqlDbType.VarChar, 250, "ObjectName").Value = thisPermissionDto.ObjectName;
                parameters.Add("@FriendlyID", SqlDbType.VarChar, 8, "FriendlyID").Value = thisPermissionDto.FriendlyID;
                parameters.Add("@Description", SqlDbType.VarChar, 250, "Description").Value = thisPermissionDto.Description;
                parameters.Add("@PermissionID", SqlDbType.UniqueIdentifier, 50, "PermissionID").Value = thisPermissionDto.PermissionID;
                parameters.Add("@ObjectType", SqlDbType.VarChar, 50, "ObjectType").Value = thisPermissionDto.ObjectType;
                var TimeStampParameter = parameters.Add("@TimeStamp", SqlDbType.Timestamp, -1, "TimeStamp");
                TimeStampParameter.Value = thisPermissionDto.TimeStamp;
                TimeStampParameter.Direction = thisDto.State == DtoChangedState.Modified ? ParameterDirection.InputOutput : ParameterDirection.Output;
            }
            else if (thisDto is RoleDto)
            {
                var thisRoleDto = thisDto as RoleDto;
                parameters.Add("@Name", SqlDbType.VarChar, 50, "Name").Value = thisRoleDto.Name;
                parameters.Add("@Description", SqlDbType.VarChar, 250, "Description").Value = thisRoleDto.Description;
                parameters.Add("@FriendlyID", SqlDbType.VarChar, 8, "FriendlyID").Value = thisRoleDto.FriendlyID;
                parameters.Add("@RoleID", SqlDbType.UniqueIdentifier, 50, "RoleID").Value = thisRoleDto.RoleID;
                var TimeStampParameter = parameters.Add("@TimeStamp", SqlDbType.Timestamp, -1, "TimeStamp");
                TimeStampParameter.Value = thisRoleDto.TimeStamp;
                TimeStampParameter.Direction = thisDto.State == DtoChangedState.Modified ? ParameterDirection.InputOutput : ParameterDirection.Output;
            }
            else if (thisDto is User_DeniedPermissionDto)
            {
                var thisUser_DeniedPermissionDto = thisDto as User_DeniedPermissionDto;
                parameters.Add("@User_DeniedPermissionID", SqlDbType.UniqueIdentifier, 50, "User_DeniedPermissionID").Value = thisUser_DeniedPermissionDto.User_DeniedPermissionID;
                parameters.Add("@UserID", SqlDbType.UniqueIdentifier, 50, "UserID").Value = thisUser_DeniedPermissionDto.UserID;
                parameters.Add("@DeniedPermissionID", SqlDbType.UniqueIdentifier, 50, "DeniedPermissionID").Value = thisUser_DeniedPermissionDto.DeniedPermissionID;
                var TimeStampParameter = parameters.Add("@TimeStamp", SqlDbType.Timestamp, -1, "TimeStamp");
                TimeStampParameter.Value = thisUser_DeniedPermissionDto.TimeStamp;
                TimeStampParameter.Direction = thisDto.State == DtoChangedState.Modified ? ParameterDirection.InputOutput : ParameterDirection.Output;
            }
            else if (thisDto is AggregateDefDto)
            {
                var thisAggregateDefDto = thisDto as AggregateDefDto;
                parameters.Add("@AggregateDefID", SqlDbType.UniqueIdentifier, 50, "AggregateDefID").Value = thisAggregateDefDto.AggregateDefID;
                parameters.Add("@Notes", SqlDbType.VarChar, 50, "Notes").Value = thisAggregateDefDto.Notes;
                parameters.Add("@EntityDefID", SqlDbType.UniqueIdentifier, 50, "EntityDefID").Value = thisAggregateDefDto.EntityDefID;
                parameters.Add("@Name", SqlDbType.VarChar, 50, "Name").Value = thisAggregateDefDto.Name;
                parameters.Add("@FriendlyID", SqlDbType.VarChar, 8, "FriendlyID").Value = thisAggregateDefDto.FriendlyID;
                parameters.Add("@DataElementID", SqlDbType.UniqueIdentifier, 50, "DataElementID").Value = thisAggregateDefDto.DataElementID;
                parameters.Add("@Description", SqlDbType.VarChar, 50, "Description").Value = thisAggregateDefDto.Description;
                parameters.Add("@RootIsCollection", SqlDbType.Bit, 50, "RootIsCollection").Value = thisAggregateDefDto.RootIsCollection;
                parameters.Add("@CreateStoredProcedure", SqlDbType.Bit, 50, "CreateStoredProcedure").Value = thisAggregateDefDto.CreateStoredProcedure;
                var TimeStampParameter = parameters.Add("@TimeStamp", SqlDbType.Timestamp, -1, "TimeStamp");
                TimeStampParameter.Value = thisAggregateDefDto.TimeStamp;
                TimeStampParameter.Direction = thisDto.State == DtoChangedState.Modified ? ParameterDirection.InputOutput : ParameterDirection.Output;
            }
            else if (thisDto is DataElementDto)
            {
                var thisDataElementDto = thisDto as DataElementDto;
                parameters.Add("@IsUpdateable", SqlDbType.Bit, 50, "IsUpdateable").Value = thisDataElementDto.IsUpdateable;
                parameters.Add("@ParentDataElementID", SqlDbType.UniqueIdentifier, 50, "ParentDataElementID").Value = thisDataElementDto.ParentDataElementID;
                parameters.Add("@DataElementID", SqlDbType.UniqueIdentifier, 50, "DataElementID").Value = thisDataElementDto.DataElementID;
                parameters.Add("@FriendlyID", SqlDbType.VarChar, 8, "FriendlyID").Value = thisDataElementDto.FriendlyID;
                parameters.Add("@IncludeAllScalarTypeProperties", SqlDbType.Bit, 50, "IncludeAllScalarTypeProperties").Value = thisDataElementDto.IncludeAllScalarTypeProperties;
                parameters.Add("@Name", SqlDbType.VarChar, 50, "Name").Value = thisDataElementDto.Name;
                parameters.Add("@AggregateDefID", SqlDbType.UniqueIdentifier, 50, "AggregateDefID").Value = thisDataElementDto.AggregateDefID;
                parameters.Add("@PropertyDefID", SqlDbType.UniqueIdentifier, 50, "PropertyDefID").Value = thisDataElementDto.PropertyDefID;
                var TimeStampParameter = parameters.Add("@TimeStamp", SqlDbType.Timestamp, -1, "TimeStamp");
                TimeStampParameter.Value = thisDataElementDto.TimeStamp;
                TimeStampParameter.Direction = thisDto.State == DtoChangedState.Modified ? ParameterDirection.InputOutput : ParameterDirection.Output;
            }
            else if (thisDto is DatabaseDto)
            {
                var thisDatabaseDto = thisDto as DatabaseDto;
                parameters.Add("@Password", SqlDbType.VarChar, 50, "Password").Value = thisDatabaseDto.Password;
                parameters.Add("@UserName", SqlDbType.VarChar, 50, "UserName").Value = thisDatabaseDto.UserName;
                parameters.Add("@DatabaseID", SqlDbType.UniqueIdentifier, 50, "DatabaseID").Value = thisDatabaseDto.DatabaseID;
                parameters.Add("@Notes", SqlDbType.VarChar, 50, "Notes").Value = thisDatabaseDto.Notes;
                parameters.Add("@Name", SqlDbType.VarChar, 50, "Name").Value = thisDatabaseDto.Name;
                parameters.Add("@Server", SqlDbType.VarChar, 50, "Server").Value = thisDatabaseDto.Server;
                parameters.Add("@FriendlyID", SqlDbType.VarChar, 8, "FriendlyID").Value = thisDatabaseDto.FriendlyID;
                parameters.Add("@UserFriendlyName", SqlDbType.VarChar, 50, "UserFriendlyName").Value = thisDatabaseDto.UserFriendlyName;
                var TimeStampParameter = parameters.Add("@TimeStamp", SqlDbType.Timestamp, -1, "TimeStamp");
                TimeStampParameter.Value = thisDatabaseDto.TimeStamp;
                TimeStampParameter.Direction = thisDto.State == DtoChangedState.Modified ? ParameterDirection.InputOutput : ParameterDirection.Output;
            }
            else if (thisDto is User_RoleDto)
            {
                var thisUser_RoleDto = thisDto as User_RoleDto;
                parameters.Add("@User_RoleID", SqlDbType.UniqueIdentifier, 50, "User_RoleID").Value = thisUser_RoleDto.User_RoleID;
                parameters.Add("@RoleID", SqlDbType.UniqueIdentifier, 50, "RoleID").Value = thisUser_RoleDto.RoleID;
                parameters.Add("@UserID", SqlDbType.UniqueIdentifier, 50, "UserID").Value = thisUser_RoleDto.UserID;
                var TimeStampParameter = parameters.Add("@TimeStamp", SqlDbType.Timestamp, -1, "TimeStamp");
                TimeStampParameter.Value = thisUser_RoleDto.TimeStamp;
                TimeStampParameter.Direction = thisDto.State == DtoChangedState.Modified ? ParameterDirection.InputOutput : ParameterDirection.Output;
            }
            else if (thisDto is UserDto)
            {
                var thisUserDto = thisDto as UserDto;
                parameters.Add("@FriendlyID", SqlDbType.VarChar, 8, "FriendlyID").Value = thisUserDto.FriendlyID;
                parameters.Add("@Password", SqlDbType.VarChar, 150, "Password").Value = thisUserDto.Password;
                parameters.Add("@UserID", SqlDbType.UniqueIdentifier, 50, "UserID").Value = thisUserDto.UserID;
                parameters.Add("@UserName", SqlDbType.VarChar, 100, "UserName").Value = thisUserDto.UserName;
                var TimeStampParameter = parameters.Add("@TimeStamp", SqlDbType.Timestamp, -1, "TimeStamp");
                TimeStampParameter.Value = thisUserDto.TimeStamp;
                TimeStampParameter.Direction = thisDto.State == DtoChangedState.Modified ? ParameterDirection.InputOutput : ParameterDirection.Output;
            }
		}

		#endregion

		#region CreateDeleteParameters

        /// <summary>
        /// Creates the parameters used for a delete operation for the identified DTO, and adds
        /// them to the identified parameters collection.
        /// </summary>
		public static void CreateDeleteParameters(this IDto thisDto, SqlParameterCollection parameters)
		{
            if (thisDto is Role_PermissionDto)
            {
                var thisRole_PermissionDto = thisDto as Role_PermissionDto;
                parameters.Add("@Role_PermissionID", SqlDbType.UniqueIdentifier, -1, "Role_PermissionID").Value = thisRole_PermissionDto.ID;
            }
            else if (thisDto is EntityDefDto)
            {
                var thisEntityDefDto = thisDto as EntityDefDto;
                parameters.Add("@EntityDefID", SqlDbType.UniqueIdentifier, -1, "EntityDefID").Value = thisEntityDefDto.ID;
            }
            else if (thisDto is DomainDto)
            {
                var thisDomainDto = thisDto as DomainDto;
                parameters.Add("@DomainID", SqlDbType.UniqueIdentifier, -1, "DomainID").Value = thisDomainDto.ID;
            }
            else if (thisDto is FilterRelationDto)
            {
                var thisFilterRelationDto = thisDto as FilterRelationDto;
                parameters.Add("@FilterRelationID", SqlDbType.UniqueIdentifier, -1, "FilterRelationID").Value = thisFilterRelationDto.ID;
            }
            else if (thisDto is PropertyDefDto)
            {
                var thisPropertyDefDto = thisDto as PropertyDefDto;
                parameters.Add("@PropertyDefID", SqlDbType.UniqueIdentifier, -1, "PropertyDefID").Value = thisPropertyDefDto.ID;
            }
            else if (thisDto is User_PermissionDto)
            {
                var thisUser_PermissionDto = thisDto as User_PermissionDto;
                parameters.Add("@User_PermissionID", SqlDbType.UniqueIdentifier, -1, "User_PermissionID").Value = thisUser_PermissionDto.ID;
            }
            else if (thisDto is PermissionDto)
            {
                var thisPermissionDto = thisDto as PermissionDto;
                parameters.Add("@PermissionID", SqlDbType.UniqueIdentifier, -1, "PermissionID").Value = thisPermissionDto.ID;
            }
            else if (thisDto is RoleDto)
            {
                var thisRoleDto = thisDto as RoleDto;
                parameters.Add("@RoleID", SqlDbType.UniqueIdentifier, -1, "RoleID").Value = thisRoleDto.ID;
            }
            else if (thisDto is User_DeniedPermissionDto)
            {
                var thisUser_DeniedPermissionDto = thisDto as User_DeniedPermissionDto;
                parameters.Add("@User_DeniedPermissionID", SqlDbType.UniqueIdentifier, -1, "User_DeniedPermissionID").Value = thisUser_DeniedPermissionDto.ID;
            }
            else if (thisDto is AggregateDefDto)
            {
                var thisAggregateDefDto = thisDto as AggregateDefDto;
                parameters.Add("@AggregateDefID", SqlDbType.UniqueIdentifier, -1, "AggregateDefID").Value = thisAggregateDefDto.ID;
            }
            else if (thisDto is DataElementDto)
            {
                var thisDataElementDto = thisDto as DataElementDto;
                parameters.Add("@DataElementID", SqlDbType.UniqueIdentifier, -1, "DataElementID").Value = thisDataElementDto.ID;
            }
            else if (thisDto is DatabaseDto)
            {
                var thisDatabaseDto = thisDto as DatabaseDto;
                parameters.Add("@DatabaseID", SqlDbType.UniqueIdentifier, -1, "DatabaseID").Value = thisDatabaseDto.ID;
            }
            else if (thisDto is User_RoleDto)
            {
                var thisUser_RoleDto = thisDto as User_RoleDto;
                parameters.Add("@User_RoleID", SqlDbType.UniqueIdentifier, -1, "User_RoleID").Value = thisUser_RoleDto.ID;
            }
            else if (thisDto is UserDto)
            {
                var thisUserDto = thisDto as UserDto;
                parameters.Add("@UserID", SqlDbType.UniqueIdentifier, -1, "UserID").Value = thisUserDto.ID;
            }
		}

		#endregion

		#region Stored Procedure Names

        /// <summary>
        /// Gets the name of the stored procedure used to insert the data from the identified DTO.
        /// </summary>
		public static string GetInsertStoredProcedureName(this IDto thisDto)
		{
            if (thisDto is Role_PermissionDto)
                return "Insert_Role_Permission";
            else if (thisDto is EntityDefDto)
                return "Insert_EntityDef";
            else if (thisDto is DomainDto)
                return "Insert_Domain";
            else if (thisDto is FilterRelationDto)
                return "Insert_FilterRelation";
            else if (thisDto is PropertyDefDto)
                return "Insert_PropertyDef";
            else if (thisDto is User_PermissionDto)
                return "Insert_User_Permission";
            else if (thisDto is PermissionDto)
                return "Insert_Permission";
            else if (thisDto is RoleDto)
                return "Insert_Role";
            else if (thisDto is User_DeniedPermissionDto)
                return "Insert_User_DeniedPermission";
            else if (thisDto is AggregateDefDto)
                return "Insert_AggregateDef";
            else if (thisDto is DataElementDto)
                return "Insert_DataElement";
            else if (thisDto is DatabaseDto)
                return "Insert_Database";
            else if (thisDto is User_RoleDto)
                return "Insert_User_Role";
            else if (thisDto is UserDto)
                return "Insert_User";
            else return string.Empty;
		}

        /// <summary>
        /// Gets the name of the stored procedure used to Update the data from the identified DTO.
        /// </summary>
		public static string GetUpdateStoredProcedureName(this IDto thisDto)
		{
            if (thisDto is Role_PermissionDto)
                return "Update_Role_Permission";
            else if (thisDto is EntityDefDto)
                return "Update_EntityDef";
            else if (thisDto is DomainDto)
                return "Update_Domain";
            else if (thisDto is FilterRelationDto)
                return "Update_FilterRelation";
            else if (thisDto is PropertyDefDto)
                return "Update_PropertyDef";
            else if (thisDto is User_PermissionDto)
                return "Update_User_Permission";
            else if (thisDto is PermissionDto)
                return "Update_Permission";
            else if (thisDto is RoleDto)
                return "Update_Role";
            else if (thisDto is User_DeniedPermissionDto)
                return "Update_User_DeniedPermission";
            else if (thisDto is AggregateDefDto)
                return "Update_AggregateDef";
            else if (thisDto is DataElementDto)
                return "Update_DataElement";
            else if (thisDto is DatabaseDto)
                return "Update_Database";
            else if (thisDto is User_RoleDto)
                return "Update_User_Role";
            else if (thisDto is UserDto)
                return "Update_User";
            else return string.Empty;
		}

        /// <summary>
        /// Gets the name of the stored procedure used to Delete the data from the identified DTO.
        /// </summary>
		public static string GetDeleteStoredProcedureName(this IDto thisDto)
		{
            if (thisDto is Role_PermissionDto)
                return "Delete_Role_Permission";
            else if (thisDto is EntityDefDto)
                return "Delete_EntityDef";
            else if (thisDto is DomainDto)
                return "Delete_Domain";
            else if (thisDto is FilterRelationDto)
                return "Delete_FilterRelation";
            else if (thisDto is PropertyDefDto)
                return "Delete_PropertyDef";
            else if (thisDto is User_PermissionDto)
                return "Delete_User_Permission";
            else if (thisDto is PermissionDto)
                return "Delete_Permission";
            else if (thisDto is RoleDto)
                return "Delete_Role";
            else if (thisDto is User_DeniedPermissionDto)
                return "Delete_User_DeniedPermission";
            else if (thisDto is AggregateDefDto)
                return "Delete_AggregateDef";
            else if (thisDto is DataElementDto)
                return "Delete_DataElement";
            else if (thisDto is DatabaseDto)
                return "Delete_Database";
            else if (thisDto is User_RoleDto)
                return "Delete_User_Role";
            else if (thisDto is UserDto)
                return "Delete_User";
            else return string.Empty;
		}

		#endregion

		#region CreateDtosFromDataReader

        /// <summary>
        /// Reads the Role_Permission data from the identifed SqlDataReader, and creates & adds
        /// respective new Role_PermissionDto objects to the identified items collection.
        /// </summary>
		internal static void CreateRole_PermissionDtos(this SqlDataReader sqlDataReader, List<Role_PermissionDto> items)
		{
            int RoleIDColumnIndex = GetColumnIndex(sqlDataReader, "RoleID");
            int PermissionIDColumnIndex = GetColumnIndex(sqlDataReader, "PermissionID");
            int Role_PermissionIDColumnIndex = GetColumnIndex(sqlDataReader, "Role_PermissionID");
            int TimeStampColumnIndex = GetColumnIndex(sqlDataReader, "TimeStamp");
            
            while (sqlDataReader.Read())
            {
                var ThisDto = new Role_PermissionDto();
                if (TimeStampColumnIndex != -1) ThisDto.TimeStamp = (byte[])sqlDataReader.GetValue(TimeStampColumnIndex);
            
                if (RoleIDColumnIndex != -1) ThisDto.RoleID = sqlDataReader.GetGuid(RoleIDColumnIndex);
                if (PermissionIDColumnIndex != -1) ThisDto.PermissionID = sqlDataReader.GetGuid(PermissionIDColumnIndex);
                if (Role_PermissionIDColumnIndex != -1) ThisDto.Role_PermissionID = sqlDataReader.GetGuid(Role_PermissionIDColumnIndex);
            
                items.Add(ThisDto);
            }
		}

        /// <summary>
        /// Reads the EntityDef data from the identifed SqlDataReader, and creates & adds
        /// respective new EntityDefDto objects to the identified items collection.
        /// </summary>
		internal static void CreateEntityDefDtos(this SqlDataReader sqlDataReader, List<EntityDefDto> items)
		{
            int TableNameColumnIndex = GetColumnIndex(sqlDataReader, "TableName");
            int PurposeColumnIndex = GetColumnIndex(sqlDataReader, "Purpose");
            int FriendlyNameColumnIndex = GetColumnIndex(sqlDataReader, "FriendlyName");
            int FilterRelationIDColumnIndex = GetColumnIndex(sqlDataReader, "FilterRelationID");
            int IsNameKeywordColumnIndex = GetColumnIndex(sqlDataReader, "IsNameKeyword");
            int NameColumnIndex = GetColumnIndex(sqlDataReader, "Name");
            int SubFolderColumnIndex = GetColumnIndex(sqlDataReader, "SubFolder");
            int CanBeAggregateRootColumnIndex = GetColumnIndex(sqlDataReader, "CanBeAggregateRoot");
            int FriendlyIDColumnIndex = GetColumnIndex(sqlDataReader, "FriendlyID");
            int EntityDefIDColumnIndex = GetColumnIndex(sqlDataReader, "EntityDefID");
            int DomainIDColumnIndex = GetColumnIndex(sqlDataReader, "DomainID");
            int TimeStampColumnIndex = GetColumnIndex(sqlDataReader, "TimeStamp");
            
            while (sqlDataReader.Read())
            {
                var ThisDto = new EntityDefDto();
                if (TimeStampColumnIndex != -1) ThisDto.TimeStamp = (byte[])sqlDataReader.GetValue(TimeStampColumnIndex);
            
                if (TableNameColumnIndex != -1) ThisDto.TableName = sqlDataReader.GetString(TableNameColumnIndex);
                if (PurposeColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(PurposeColumnIndex))
                        ThisDto.Purpose = null;
                    else
                        ThisDto.Purpose = sqlDataReader.GetString(PurposeColumnIndex);
                }
                if (FriendlyNameColumnIndex != -1) ThisDto.FriendlyName = sqlDataReader.GetString(FriendlyNameColumnIndex);
                if (FilterRelationIDColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(FilterRelationIDColumnIndex))
                        ThisDto.FilterRelationID = null;
                    else
                        ThisDto.FilterRelationID = sqlDataReader.GetGuid(FilterRelationIDColumnIndex);
                }
                if (IsNameKeywordColumnIndex != -1) ThisDto.IsNameKeyword = sqlDataReader.GetBoolean(IsNameKeywordColumnIndex);
                if (NameColumnIndex != -1) ThisDto.Name = sqlDataReader.GetString(NameColumnIndex);
                if (SubFolderColumnIndex != -1) ThisDto.SubFolder = sqlDataReader.GetString(SubFolderColumnIndex);
                if (CanBeAggregateRootColumnIndex != -1) ThisDto.CanBeAggregateRoot = sqlDataReader.GetBoolean(CanBeAggregateRootColumnIndex);
                if (FriendlyIDColumnIndex != -1) ThisDto.FriendlyID = sqlDataReader.GetString(FriendlyIDColumnIndex);
                if (EntityDefIDColumnIndex != -1) ThisDto.EntityDefID = sqlDataReader.GetGuid(EntityDefIDColumnIndex);
                if (DomainIDColumnIndex != -1) ThisDto.DomainID = sqlDataReader.GetGuid(DomainIDColumnIndex);
            
                items.Add(ThisDto);
            }
		}

        /// <summary>
        /// Reads the Domain data from the identifed SqlDataReader, and creates & adds respective
        /// new DomainDto objects to the identified items collection.
        /// </summary>
		internal static void CreateDomainDtos(this SqlDataReader sqlDataReader, List<DomainDto> items)
		{
            int NamespaceColumnIndex = GetColumnIndex(sqlDataReader, "Namespace");
            int ProjectNameColumnIndex = GetColumnIndex(sqlDataReader, "ProjectName");
            int ProductionDatabaseIDColumnIndex = GetColumnIndex(sqlDataReader, "ProductionDatabaseID");
            int NotesColumnIndex = GetColumnIndex(sqlDataReader, "Notes");
            int FriendlyIDColumnIndex = GetColumnIndex(sqlDataReader, "FriendlyID");
            int DevelopmentDatabaseIDColumnIndex = GetColumnIndex(sqlDataReader, "DevelopmentDatabaseID");
            int ProjectRelativePathColumnIndex = GetColumnIndex(sqlDataReader, "ProjectRelativePath");
            int DomainIDColumnIndex = GetColumnIndex(sqlDataReader, "DomainID");
            int NameColumnIndex = GetColumnIndex(sqlDataReader, "Name");
            int ServicesProjectRelativePathColumnIndex = GetColumnIndex(sqlDataReader, "ServicesProjectRelativePath");
            int ServicesProjectNameColumnIndex = GetColumnIndex(sqlDataReader, "ServicesProjectName");
            int TimeStampColumnIndex = GetColumnIndex(sqlDataReader, "TimeStamp");
            
            while (sqlDataReader.Read())
            {
                var ThisDto = new DomainDto();
                if (TimeStampColumnIndex != -1) ThisDto.TimeStamp = (byte[])sqlDataReader.GetValue(TimeStampColumnIndex);
            
                if (NamespaceColumnIndex != -1) ThisDto.Namespace = sqlDataReader.GetString(NamespaceColumnIndex);
                if (ProjectNameColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(ProjectNameColumnIndex))
                        ThisDto.ProjectName = null;
                    else
                        ThisDto.ProjectName = sqlDataReader.GetString(ProjectNameColumnIndex);
                }
                if (ProductionDatabaseIDColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(ProductionDatabaseIDColumnIndex))
                        ThisDto.ProductionDatabaseID = null;
                    else
                        ThisDto.ProductionDatabaseID = sqlDataReader.GetGuid(ProductionDatabaseIDColumnIndex);
                }
                if (NotesColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(NotesColumnIndex))
                        ThisDto.Notes = null;
                    else
                        ThisDto.Notes = sqlDataReader.GetString(NotesColumnIndex);
                }
                if (FriendlyIDColumnIndex != -1) ThisDto.FriendlyID = sqlDataReader.GetString(FriendlyIDColumnIndex);
                if (DevelopmentDatabaseIDColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(DevelopmentDatabaseIDColumnIndex))
                        ThisDto.DevelopmentDatabaseID = null;
                    else
                        ThisDto.DevelopmentDatabaseID = sqlDataReader.GetGuid(DevelopmentDatabaseIDColumnIndex);
                }
                if (ProjectRelativePathColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(ProjectRelativePathColumnIndex))
                        ThisDto.ProjectRelativePath = null;
                    else
                        ThisDto.ProjectRelativePath = sqlDataReader.GetString(ProjectRelativePathColumnIndex);
                }
                if (DomainIDColumnIndex != -1) ThisDto.DomainID = sqlDataReader.GetGuid(DomainIDColumnIndex);
                if (NameColumnIndex != -1) ThisDto.Name = sqlDataReader.GetString(NameColumnIndex);
                if (ServicesProjectRelativePathColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(ServicesProjectRelativePathColumnIndex))
                        ThisDto.ServicesProjectRelativePath = null;
                    else
                        ThisDto.ServicesProjectRelativePath = sqlDataReader.GetString(ServicesProjectRelativePathColumnIndex);
                }
                if (ServicesProjectNameColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(ServicesProjectNameColumnIndex))
                        ThisDto.ServicesProjectName = null;
                    else
                        ThisDto.ServicesProjectName = sqlDataReader.GetString(ServicesProjectNameColumnIndex);
                }
            
                items.Add(ThisDto);
            }
		}

        /// <summary>
        /// Reads the FilterRelation data from the identifed SqlDataReader, and creates & adds
        /// respective new FilterRelationDto objects to the identified items collection.
        /// </summary>
		internal static void CreateFilterRelationDtos(this SqlDataReader sqlDataReader, List<FilterRelationDto> items)
		{
            int EntityDefIDColumnIndex = GetColumnIndex(sqlDataReader, "EntityDefID");
            int PropertyDefIDColumnIndex = GetColumnIndex(sqlDataReader, "PropertyDefID");
            int ParentFilterRelationIDColumnIndex = GetColumnIndex(sqlDataReader, "ParentFilterRelationID");
            int FriendlyIDColumnIndex = GetColumnIndex(sqlDataReader, "FriendlyID");
            int FilterRelationIDColumnIndex = GetColumnIndex(sqlDataReader, "FilterRelationID");
            int TimeStampColumnIndex = GetColumnIndex(sqlDataReader, "TimeStamp");
            
            while (sqlDataReader.Read())
            {
                var ThisDto = new FilterRelationDto();
                if (TimeStampColumnIndex != -1) ThisDto.TimeStamp = (byte[])sqlDataReader.GetValue(TimeStampColumnIndex);
            
                if (EntityDefIDColumnIndex != -1) ThisDto.EntityDefID = sqlDataReader.GetGuid(EntityDefIDColumnIndex);
                if (PropertyDefIDColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(PropertyDefIDColumnIndex))
                        ThisDto.PropertyDefID = null;
                    else
                        ThisDto.PropertyDefID = sqlDataReader.GetGuid(PropertyDefIDColumnIndex);
                }
                if (ParentFilterRelationIDColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(ParentFilterRelationIDColumnIndex))
                        ThisDto.ParentFilterRelationID = null;
                    else
                        ThisDto.ParentFilterRelationID = sqlDataReader.GetGuid(ParentFilterRelationIDColumnIndex);
                }
                if (FriendlyIDColumnIndex != -1) ThisDto.FriendlyID = sqlDataReader.GetString(FriendlyIDColumnIndex);
                if (FilterRelationIDColumnIndex != -1) ThisDto.FilterRelationID = sqlDataReader.GetGuid(FilterRelationIDColumnIndex);
            
                items.Add(ThisDto);
            }
		}

        /// <summary>
        /// Reads the PropertyDef data from the identifed SqlDataReader, and creates & adds
        /// respective new PropertyDefDto objects to the identified items collection.
        /// </summary>
		internal static void CreatePropertyDefDtos(this SqlDataReader sqlDataReader, List<PropertyDefDto> items)
		{
            int IsNameKeywordColumnIndex = GetColumnIndex(sqlDataReader, "IsNameKeyword");
            int IsPrimaryKeyColumnIndex = GetColumnIndex(sqlDataReader, "IsPrimaryKey");
            int PurposeColumnIndex = GetColumnIndex(sqlDataReader, "Purpose");
            int SqlDataPrecisionColumnIndex = GetColumnIndex(sqlDataReader, "SqlDataPrecision");
            int EntityDefIDColumnIndex = GetColumnIndex(sqlDataReader, "EntityDefID");
            int SqlDataScaleColumnIndex = GetColumnIndex(sqlDataReader, "SqlDataScale");
            int PropertyDefIDColumnIndex = GetColumnIndex(sqlDataReader, "PropertyDefID");
            int SqlDefaultValueColumnIndex = GetColumnIndex(sqlDataReader, "SqlDefaultValue");
            int DefaultValueColumnIndex = GetColumnIndex(sqlDataReader, "DefaultValue");
            int DatabaseColumnNameColumnIndex = GetColumnIndex(sqlDataReader, "DatabaseColumnName");
            int ForeignKeyPropertyDefIDColumnIndex = GetColumnIndex(sqlDataReader, "ForeignKeyPropertyDefID");
            int NameColumnIndex = GetColumnIndex(sqlDataReader, "Name");
            int FriendlyNameColumnIndex = GetColumnIndex(sqlDataReader, "FriendlyName");
            int IsFriendlyIDColumnIndex = GetColumnIndex(sqlDataReader, "IsFriendlyID");
            int SqlDataTypeColumnIndex = GetColumnIndex(sqlDataReader, "SqlDataType");
            int DataTypeColumnIndex = GetColumnIndex(sqlDataReader, "DataType");
            int IsNullableColumnIndex = GetColumnIndex(sqlDataReader, "IsNullable");
            int FriendlyIDColumnIndex = GetColumnIndex(sqlDataReader, "FriendlyID");
            int SqlIsIdentityColumnIndex = GetColumnIndex(sqlDataReader, "SqlIsIdentity");
            int SqlDataLengthColumnIndex = GetColumnIndex(sqlDataReader, "SqlDataLength");
            int TimeStampColumnIndex = GetColumnIndex(sqlDataReader, "TimeStamp");
            
            while (sqlDataReader.Read())
            {
                var ThisDto = new PropertyDefDto();
                if (TimeStampColumnIndex != -1) ThisDto.TimeStamp = (byte[])sqlDataReader.GetValue(TimeStampColumnIndex);
            
                if (IsNameKeywordColumnIndex != -1) ThisDto.IsNameKeyword = sqlDataReader.GetBoolean(IsNameKeywordColumnIndex);
                if (IsPrimaryKeyColumnIndex != -1) ThisDto.IsPrimaryKey = sqlDataReader.GetBoolean(IsPrimaryKeyColumnIndex);
                if (PurposeColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(PurposeColumnIndex))
                        ThisDto.Purpose = null;
                    else
                        ThisDto.Purpose = sqlDataReader.GetString(PurposeColumnIndex);
                }
                if (SqlDataPrecisionColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(SqlDataPrecisionColumnIndex))
                        ThisDto.SqlDataPrecision = null;
                    else
                        ThisDto.SqlDataPrecision = sqlDataReader.GetInt32(SqlDataPrecisionColumnIndex);
                }
                if (EntityDefIDColumnIndex != -1) ThisDto.EntityDefID = sqlDataReader.GetGuid(EntityDefIDColumnIndex);
                if (SqlDataScaleColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(SqlDataScaleColumnIndex))
                        ThisDto.SqlDataScale = null;
                    else
                        ThisDto.SqlDataScale = sqlDataReader.GetInt32(SqlDataScaleColumnIndex);
                }
                if (PropertyDefIDColumnIndex != -1) ThisDto.PropertyDefID = sqlDataReader.GetGuid(PropertyDefIDColumnIndex);
                if (SqlDefaultValueColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(SqlDefaultValueColumnIndex))
                        ThisDto.SqlDefaultValue = null;
                    else
                        ThisDto.SqlDefaultValue = sqlDataReader.GetString(SqlDefaultValueColumnIndex);
                }
                if (DefaultValueColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(DefaultValueColumnIndex))
                        ThisDto.DefaultValue = null;
                    else
                        ThisDto.DefaultValue = sqlDataReader.GetString(DefaultValueColumnIndex);
                }
                if (DatabaseColumnNameColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(DatabaseColumnNameColumnIndex))
                        ThisDto.DatabaseColumnName = null;
                    else
                        ThisDto.DatabaseColumnName = sqlDataReader.GetString(DatabaseColumnNameColumnIndex);
                }
                if (ForeignKeyPropertyDefIDColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(ForeignKeyPropertyDefIDColumnIndex))
                        ThisDto.ForeignKeyPropertyDefID = null;
                    else
                        ThisDto.ForeignKeyPropertyDefID = sqlDataReader.GetGuid(ForeignKeyPropertyDefIDColumnIndex);
                }
                if (NameColumnIndex != -1) ThisDto.Name = sqlDataReader.GetString(NameColumnIndex);
                if (FriendlyNameColumnIndex != -1) ThisDto.FriendlyName = sqlDataReader.GetString(FriendlyNameColumnIndex);
                if (IsFriendlyIDColumnIndex != -1) ThisDto.IsFriendlyID = sqlDataReader.GetBoolean(IsFriendlyIDColumnIndex);
                if (SqlDataTypeColumnIndex != -1) ThisDto.SqlDataType = sqlDataReader.GetString(SqlDataTypeColumnIndex);
                if (DataTypeColumnIndex != -1) ThisDto.DataType = sqlDataReader.GetString(DataTypeColumnIndex);
                if (IsNullableColumnIndex != -1) ThisDto.IsNullable = sqlDataReader.GetBoolean(IsNullableColumnIndex);
                if (FriendlyIDColumnIndex != -1) ThisDto.FriendlyID = sqlDataReader.GetString(FriendlyIDColumnIndex);
                if (SqlIsIdentityColumnIndex != -1) ThisDto.SqlIsIdentity = sqlDataReader.GetBoolean(SqlIsIdentityColumnIndex);
                if (SqlDataLengthColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(SqlDataLengthColumnIndex))
                        ThisDto.SqlDataLength = null;
                    else
                        ThisDto.SqlDataLength = sqlDataReader.GetInt32(SqlDataLengthColumnIndex);
                }
            
                items.Add(ThisDto);
            }
		}

        /// <summary>
        /// Reads the User_Permission data from the identifed SqlDataReader, and creates & adds
        /// respective new User_PermissionDto objects to the identified items collection.
        /// </summary>
		internal static void CreateUser_PermissionDtos(this SqlDataReader sqlDataReader, List<User_PermissionDto> items)
		{
            int PermissionIDColumnIndex = GetColumnIndex(sqlDataReader, "PermissionID");
            int User_PermissionIDColumnIndex = GetColumnIndex(sqlDataReader, "User_PermissionID");
            int UserIDColumnIndex = GetColumnIndex(sqlDataReader, "UserID");
            int TimeStampColumnIndex = GetColumnIndex(sqlDataReader, "TimeStamp");
            
            while (sqlDataReader.Read())
            {
                var ThisDto = new User_PermissionDto();
                if (TimeStampColumnIndex != -1) ThisDto.TimeStamp = (byte[])sqlDataReader.GetValue(TimeStampColumnIndex);
            
                if (PermissionIDColumnIndex != -1) ThisDto.PermissionID = sqlDataReader.GetGuid(PermissionIDColumnIndex);
                if (User_PermissionIDColumnIndex != -1) ThisDto.User_PermissionID = sqlDataReader.GetGuid(User_PermissionIDColumnIndex);
                if (UserIDColumnIndex != -1) ThisDto.UserID = sqlDataReader.GetGuid(UserIDColumnIndex);
            
                items.Add(ThisDto);
            }
		}

        /// <summary>
        /// Reads the Permission data from the identifed SqlDataReader, and creates & adds
        /// respective new PermissionDto objects to the identified items collection.
        /// </summary>
		internal static void CreatePermissionDtos(this SqlDataReader sqlDataReader, List<PermissionDto> items)
		{
            int OperationTypeColumnIndex = GetColumnIndex(sqlDataReader, "OperationType");
            int NameColumnIndex = GetColumnIndex(sqlDataReader, "Name");
            int ObjectNameColumnIndex = GetColumnIndex(sqlDataReader, "ObjectName");
            int FriendlyIDColumnIndex = GetColumnIndex(sqlDataReader, "FriendlyID");
            int DescriptionColumnIndex = GetColumnIndex(sqlDataReader, "Description");
            int PermissionIDColumnIndex = GetColumnIndex(sqlDataReader, "PermissionID");
            int ObjectTypeColumnIndex = GetColumnIndex(sqlDataReader, "ObjectType");
            int TimeStampColumnIndex = GetColumnIndex(sqlDataReader, "TimeStamp");
            
            while (sqlDataReader.Read())
            {
                var ThisDto = new PermissionDto();
                if (TimeStampColumnIndex != -1) ThisDto.TimeStamp = (byte[])sqlDataReader.GetValue(TimeStampColumnIndex);
            
                if (OperationTypeColumnIndex != -1) ThisDto.OperationType = sqlDataReader.GetString(OperationTypeColumnIndex);
                if (NameColumnIndex != -1) ThisDto.Name = sqlDataReader.GetString(NameColumnIndex);
                if (ObjectNameColumnIndex != -1) ThisDto.ObjectName = sqlDataReader.GetString(ObjectNameColumnIndex);
                if (FriendlyIDColumnIndex != -1) ThisDto.FriendlyID = sqlDataReader.GetString(FriendlyIDColumnIndex);
                if (DescriptionColumnIndex != -1) ThisDto.Description = sqlDataReader.GetString(DescriptionColumnIndex);
                if (PermissionIDColumnIndex != -1) ThisDto.PermissionID = sqlDataReader.GetGuid(PermissionIDColumnIndex);
                if (ObjectTypeColumnIndex != -1) ThisDto.ObjectType = sqlDataReader.GetString(ObjectTypeColumnIndex);
            
                items.Add(ThisDto);
            }
		}

        /// <summary>
        /// Reads the Role data from the identifed SqlDataReader, and creates & adds respective new
        /// RoleDto objects to the identified items collection.
        /// </summary>
		internal static void CreateRoleDtos(this SqlDataReader sqlDataReader, List<RoleDto> items)
		{
            int NameColumnIndex = GetColumnIndex(sqlDataReader, "Name");
            int DescriptionColumnIndex = GetColumnIndex(sqlDataReader, "Description");
            int FriendlyIDColumnIndex = GetColumnIndex(sqlDataReader, "FriendlyID");
            int RoleIDColumnIndex = GetColumnIndex(sqlDataReader, "RoleID");
            int TimeStampColumnIndex = GetColumnIndex(sqlDataReader, "TimeStamp");
            
            while (sqlDataReader.Read())
            {
                var ThisDto = new RoleDto();
                if (TimeStampColumnIndex != -1) ThisDto.TimeStamp = (byte[])sqlDataReader.GetValue(TimeStampColumnIndex);
            
                if (NameColumnIndex != -1) ThisDto.Name = sqlDataReader.GetString(NameColumnIndex);
                if (DescriptionColumnIndex != -1) ThisDto.Description = sqlDataReader.GetString(DescriptionColumnIndex);
                if (FriendlyIDColumnIndex != -1) ThisDto.FriendlyID = sqlDataReader.GetString(FriendlyIDColumnIndex);
                if (RoleIDColumnIndex != -1) ThisDto.RoleID = sqlDataReader.GetGuid(RoleIDColumnIndex);
            
                items.Add(ThisDto);
            }
		}

        /// <summary>
        /// Reads the User_DeniedPermission data from the identifed SqlDataReader, and creates &
        /// adds respective new User_DeniedPermissionDto objects to the identified items collection.
        /// </summary>
		internal static void CreateUser_DeniedPermissionDtos(this SqlDataReader sqlDataReader, List<User_DeniedPermissionDto> items)
		{
            int User_DeniedPermissionIDColumnIndex = GetColumnIndex(sqlDataReader, "User_DeniedPermissionID");
            int UserIDColumnIndex = GetColumnIndex(sqlDataReader, "UserID");
            int DeniedPermissionIDColumnIndex = GetColumnIndex(sqlDataReader, "DeniedPermissionID");
            int TimeStampColumnIndex = GetColumnIndex(sqlDataReader, "TimeStamp");
            
            while (sqlDataReader.Read())
            {
                var ThisDto = new User_DeniedPermissionDto();
                if (TimeStampColumnIndex != -1) ThisDto.TimeStamp = (byte[])sqlDataReader.GetValue(TimeStampColumnIndex);
            
                if (User_DeniedPermissionIDColumnIndex != -1) ThisDto.User_DeniedPermissionID = sqlDataReader.GetGuid(User_DeniedPermissionIDColumnIndex);
                if (UserIDColumnIndex != -1) ThisDto.UserID = sqlDataReader.GetGuid(UserIDColumnIndex);
                if (DeniedPermissionIDColumnIndex != -1) ThisDto.DeniedPermissionID = sqlDataReader.GetGuid(DeniedPermissionIDColumnIndex);
            
                items.Add(ThisDto);
            }
		}

        /// <summary>
        /// Reads the AggregateDef data from the identifed SqlDataReader, and creates & adds
        /// respective new AggregateDefDto objects to the identified items collection.
        /// </summary>
		internal static void CreateAggregateDefDtos(this SqlDataReader sqlDataReader, List<AggregateDefDto> items)
		{
            int AggregateDefIDColumnIndex = GetColumnIndex(sqlDataReader, "AggregateDefID");
            int NotesColumnIndex = GetColumnIndex(sqlDataReader, "Notes");
            int EntityDefIDColumnIndex = GetColumnIndex(sqlDataReader, "EntityDefID");
            int NameColumnIndex = GetColumnIndex(sqlDataReader, "Name");
            int FriendlyIDColumnIndex = GetColumnIndex(sqlDataReader, "FriendlyID");
            int DataElementIDColumnIndex = GetColumnIndex(sqlDataReader, "DataElementID");
            int DescriptionColumnIndex = GetColumnIndex(sqlDataReader, "Description");
            int RootIsCollectionColumnIndex = GetColumnIndex(sqlDataReader, "RootIsCollection");
            int CreateStoredProcedureColumnIndex = GetColumnIndex(sqlDataReader, "CreateStoredProcedure");
            int TimeStampColumnIndex = GetColumnIndex(sqlDataReader, "TimeStamp");
            
            while (sqlDataReader.Read())
            {
                var ThisDto = new AggregateDefDto();
                if (TimeStampColumnIndex != -1) ThisDto.TimeStamp = (byte[])sqlDataReader.GetValue(TimeStampColumnIndex);
            
                if (AggregateDefIDColumnIndex != -1) ThisDto.AggregateDefID = sqlDataReader.GetGuid(AggregateDefIDColumnIndex);
                if (NotesColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(NotesColumnIndex))
                        ThisDto.Notes = null;
                    else
                        ThisDto.Notes = sqlDataReader.GetString(NotesColumnIndex);
                }
                if (EntityDefIDColumnIndex != -1) ThisDto.EntityDefID = sqlDataReader.GetGuid(EntityDefIDColumnIndex);
                if (NameColumnIndex != -1) ThisDto.Name = sqlDataReader.GetString(NameColumnIndex);
                if (FriendlyIDColumnIndex != -1) ThisDto.FriendlyID = sqlDataReader.GetString(FriendlyIDColumnIndex);
                if (DataElementIDColumnIndex != -1) ThisDto.DataElementID = sqlDataReader.GetGuid(DataElementIDColumnIndex);
                if (DescriptionColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(DescriptionColumnIndex))
                        ThisDto.Description = null;
                    else
                        ThisDto.Description = sqlDataReader.GetString(DescriptionColumnIndex);
                }
                if (RootIsCollectionColumnIndex != -1) ThisDto.RootIsCollection = sqlDataReader.GetBoolean(RootIsCollectionColumnIndex);
                if (CreateStoredProcedureColumnIndex != -1) ThisDto.CreateStoredProcedure = sqlDataReader.GetBoolean(CreateStoredProcedureColumnIndex);
            
                items.Add(ThisDto);
            }
		}

        /// <summary>
        /// Reads the DataElement data from the identifed SqlDataReader, and creates & adds
        /// respective new DataElementDto objects to the identified items collection.
        /// </summary>
		internal static void CreateDataElementDtos(this SqlDataReader sqlDataReader, List<DataElementDto> items)
		{
            int IsUpdateableColumnIndex = GetColumnIndex(sqlDataReader, "IsUpdateable");
            int ParentDataElementIDColumnIndex = GetColumnIndex(sqlDataReader, "ParentDataElementID");
            int DataElementIDColumnIndex = GetColumnIndex(sqlDataReader, "DataElementID");
            int FriendlyIDColumnIndex = GetColumnIndex(sqlDataReader, "FriendlyID");
            int IncludeAllScalarTypePropertiesColumnIndex = GetColumnIndex(sqlDataReader, "IncludeAllScalarTypeProperties");
            int NameColumnIndex = GetColumnIndex(sqlDataReader, "Name");
            int AggregateDefIDColumnIndex = GetColumnIndex(sqlDataReader, "AggregateDefID");
            int PropertyDefIDColumnIndex = GetColumnIndex(sqlDataReader, "PropertyDefID");
            int TimeStampColumnIndex = GetColumnIndex(sqlDataReader, "TimeStamp");
            
            while (sqlDataReader.Read())
            {
                var ThisDto = new DataElementDto();
                if (TimeStampColumnIndex != -1) ThisDto.TimeStamp = (byte[])sqlDataReader.GetValue(TimeStampColumnIndex);
            
                if (IsUpdateableColumnIndex != -1) ThisDto.IsUpdateable = sqlDataReader.GetBoolean(IsUpdateableColumnIndex);
                if (ParentDataElementIDColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(ParentDataElementIDColumnIndex))
                        ThisDto.ParentDataElementID = null;
                    else
                        ThisDto.ParentDataElementID = sqlDataReader.GetGuid(ParentDataElementIDColumnIndex);
                }
                if (DataElementIDColumnIndex != -1) ThisDto.DataElementID = sqlDataReader.GetGuid(DataElementIDColumnIndex);
                if (FriendlyIDColumnIndex != -1) ThisDto.FriendlyID = sqlDataReader.GetString(FriendlyIDColumnIndex);
                if (IncludeAllScalarTypePropertiesColumnIndex != -1) ThisDto.IncludeAllScalarTypeProperties = sqlDataReader.GetBoolean(IncludeAllScalarTypePropertiesColumnIndex);
                if (NameColumnIndex != -1) ThisDto.Name = sqlDataReader.GetString(NameColumnIndex);
                if (AggregateDefIDColumnIndex != -1) ThisDto.AggregateDefID = sqlDataReader.GetGuid(AggregateDefIDColumnIndex);
                if (PropertyDefIDColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(PropertyDefIDColumnIndex))
                        ThisDto.PropertyDefID = null;
                    else
                        ThisDto.PropertyDefID = sqlDataReader.GetGuid(PropertyDefIDColumnIndex);
                }
            
                items.Add(ThisDto);
            }
		}

        /// <summary>
        /// Reads the Database data from the identifed SqlDataReader, and creates & adds respective
        /// new DatabaseDto objects to the identified items collection.
        /// </summary>
		internal static void CreateDatabaseDtos(this SqlDataReader sqlDataReader, List<DatabaseDto> items)
		{
            int PasswordColumnIndex = GetColumnIndex(sqlDataReader, "Password");
            int UserNameColumnIndex = GetColumnIndex(sqlDataReader, "UserName");
            int DatabaseIDColumnIndex = GetColumnIndex(sqlDataReader, "DatabaseID");
            int NotesColumnIndex = GetColumnIndex(sqlDataReader, "Notes");
            int NameColumnIndex = GetColumnIndex(sqlDataReader, "Name");
            int ServerColumnIndex = GetColumnIndex(sqlDataReader, "Server");
            int FriendlyIDColumnIndex = GetColumnIndex(sqlDataReader, "FriendlyID");
            int UserFriendlyNameColumnIndex = GetColumnIndex(sqlDataReader, "UserFriendlyName");
            int TimeStampColumnIndex = GetColumnIndex(sqlDataReader, "TimeStamp");
            
            while (sqlDataReader.Read())
            {
                var ThisDto = new DatabaseDto();
                if (TimeStampColumnIndex != -1) ThisDto.TimeStamp = (byte[])sqlDataReader.GetValue(TimeStampColumnIndex);
            
                if (PasswordColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(PasswordColumnIndex))
                        ThisDto.Password = null;
                    else
                        ThisDto.Password = sqlDataReader.GetString(PasswordColumnIndex);
                }
                if (UserNameColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(UserNameColumnIndex))
                        ThisDto.UserName = null;
                    else
                        ThisDto.UserName = sqlDataReader.GetString(UserNameColumnIndex);
                }
                if (DatabaseIDColumnIndex != -1) ThisDto.DatabaseID = sqlDataReader.GetGuid(DatabaseIDColumnIndex);
                if (NotesColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(NotesColumnIndex))
                        ThisDto.Notes = null;
                    else
                        ThisDto.Notes = sqlDataReader.GetString(NotesColumnIndex);
                }
                if (NameColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(NameColumnIndex))
                        ThisDto.Name = null;
                    else
                        ThisDto.Name = sqlDataReader.GetString(NameColumnIndex);
                }
                if (ServerColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(ServerColumnIndex))
                        ThisDto.Server = null;
                    else
                        ThisDto.Server = sqlDataReader.GetString(ServerColumnIndex);
                }
                if (FriendlyIDColumnIndex != -1) ThisDto.FriendlyID = sqlDataReader.GetString(FriendlyIDColumnIndex);
                if (UserFriendlyNameColumnIndex != -1)
                {
                    if (sqlDataReader.IsDBNull(UserFriendlyNameColumnIndex))
                        ThisDto.UserFriendlyName = null;
                    else
                        ThisDto.UserFriendlyName = sqlDataReader.GetString(UserFriendlyNameColumnIndex);
                }
            
                items.Add(ThisDto);
            }
		}

        /// <summary>
        /// Reads the User_Role data from the identifed SqlDataReader, and creates & adds
        /// respective new User_RoleDto objects to the identified items collection.
        /// </summary>
		internal static void CreateUser_RoleDtos(this SqlDataReader sqlDataReader, List<User_RoleDto> items)
		{
            int User_RoleIDColumnIndex = GetColumnIndex(sqlDataReader, "User_RoleID");
            int RoleIDColumnIndex = GetColumnIndex(sqlDataReader, "RoleID");
            int UserIDColumnIndex = GetColumnIndex(sqlDataReader, "UserID");
            int TimeStampColumnIndex = GetColumnIndex(sqlDataReader, "TimeStamp");
            
            while (sqlDataReader.Read())
            {
                var ThisDto = new User_RoleDto();
                if (TimeStampColumnIndex != -1) ThisDto.TimeStamp = (byte[])sqlDataReader.GetValue(TimeStampColumnIndex);
            
                if (User_RoleIDColumnIndex != -1) ThisDto.User_RoleID = sqlDataReader.GetGuid(User_RoleIDColumnIndex);
                if (RoleIDColumnIndex != -1) ThisDto.RoleID = sqlDataReader.GetGuid(RoleIDColumnIndex);
                if (UserIDColumnIndex != -1) ThisDto.UserID = sqlDataReader.GetGuid(UserIDColumnIndex);
            
                items.Add(ThisDto);
            }
		}

        /// <summary>
        /// Reads the User data from the identifed SqlDataReader, and creates & adds respective new
        /// UserDto objects to the identified items collection.
        /// </summary>
		internal static void CreateUserDtos(this SqlDataReader sqlDataReader, List<UserDto> items)
		{
            int FriendlyIDColumnIndex = GetColumnIndex(sqlDataReader, "FriendlyID");
            int PasswordColumnIndex = GetColumnIndex(sqlDataReader, "Password");
            int UserIDColumnIndex = GetColumnIndex(sqlDataReader, "UserID");
            int UserNameColumnIndex = GetColumnIndex(sqlDataReader, "UserName");
            int TimeStampColumnIndex = GetColumnIndex(sqlDataReader, "TimeStamp");
            
            while (sqlDataReader.Read())
            {
                var ThisDto = new UserDto();
                if (TimeStampColumnIndex != -1) ThisDto.TimeStamp = (byte[])sqlDataReader.GetValue(TimeStampColumnIndex);
            
                if (FriendlyIDColumnIndex != -1) ThisDto.FriendlyID = sqlDataReader.GetString(FriendlyIDColumnIndex);
                if (PasswordColumnIndex != -1) ThisDto.Password = sqlDataReader.GetString(PasswordColumnIndex);
                if (UserIDColumnIndex != -1) ThisDto.UserID = sqlDataReader.GetGuid(UserIDColumnIndex);
                if (UserNameColumnIndex != -1) ThisDto.UserName = sqlDataReader.GetString(UserNameColumnIndex);
            
                items.Add(ThisDto);
            }
		}

        /// <summary>
        /// Gets the column index within the identified SqlDataReader for the identified column
        /// name.
        /// </summary>
		private static int GetColumnIndex(SqlDataReader sqlDataReader, string columnName)
		{
            int FoundIndex = -1;
            for (int columnIndex = 0; columnIndex < sqlDataReader.FieldCount; columnIndex++)
            {
              if (sqlDataReader.GetName(columnIndex)==columnName)
              {
                  FoundIndex = columnIndex;
                  break;
              }
            }
            return FoundIndex;
		}

		#endregion

		#region GetDeleteDto

        /// <summary>
        /// Gets appropriate IDto object that can be used to delete the respective record in the
        /// database.
        /// </summary>
		public static IDto GetDeleteDto(this string className, Guid id)
		{
            if (className == "Role_Permission")
            {
                return new Role_PermissionDto() { Role_PermissionID = id };
            }
            else if (className == "EntityDef")
            {
                return new EntityDefDto() { EntityDefID = id };
            }
            else if (className == "Domain")
            {
                return new DomainDto() { DomainID = id };
            }
            else if (className == "FilterRelation")
            {
                return new FilterRelationDto() { FilterRelationID = id };
            }
            else if (className == "PropertyDef")
            {
                return new PropertyDefDto() { PropertyDefID = id };
            }
            else if (className == "User_Permission")
            {
                return new User_PermissionDto() { User_PermissionID = id };
            }
            else if (className == "Permission")
            {
                return new PermissionDto() { PermissionID = id };
            }
            else if (className == "Role")
            {
                return new RoleDto() { RoleID = id };
            }
            else if (className == "User_DeniedPermission")
            {
                return new User_DeniedPermissionDto() { User_DeniedPermissionID = id };
            }
            else if (className == "AggregateDef")
            {
                return new AggregateDefDto() { AggregateDefID = id };
            }
            else if (className == "DataElement")
            {
                return new DataElementDto() { DataElementID = id };
            }
            else if (className == "Database")
            {
                return new DatabaseDto() { DatabaseID = id };
            }
            else if (className == "User_Role")
            {
                return new User_RoleDto() { User_RoleID = id };
            }
            else if (className == "User")
            {
                return new UserDto() { UserID = id };
            }
            else
                throw new Exception("Unable to determine DTO for class name: " + className);
		}

		#endregion

		#region GetStoredProcedureCommand

        /// <summary>
        /// Gets appropriate IDto object that can be used to delete the respective record in the
        /// database.
        /// </summary>
		public static SqlCommand GetStoredProcedureCommand(string storedProcedureName, string filter, string userName, string password)
		{
            string ConnectionString = ConfigurationManager.ConnectionStrings["DomainDatabaseConnection"].ConnectionString;
            var ThisConnection = new SqlConnection(ConnectionString);
            var ThisCommand = new SqlCommand();
            ThisCommand.Connection = ThisConnection;
            ThisCommand.CommandText = storedProcedureName;
            ThisCommand.CommandType = CommandType.StoredProcedure;
            ThisCommand.Parameters.AddWithValue("@Filter", filter);
            ThisCommand.Parameters.AddWithValue("@ThisUser", userName);
            ThisCommand.Parameters.AddWithValue("@UserPassword", password);
            
            return ThisCommand;
		}

		#endregion

		#region RemoveDto

        /// <summary>
        /// Removes the identified DTO from the identified DtoHost
        /// </summary>
		public static void RemoveDto(this DtoHost ThisDtoHost, IDto dto)
		{
            if (dto is Role_PermissionDto)
            {
                var ThisDto = dto as Role_PermissionDto;
                if (ThisDtoHost.RootRole_Permissions.Contains(ThisDto))
                    ThisDtoHost.RootRole_Permissions.Remove(ThisDto);
                else if (ThisDtoHost.Role_Permissions.Contains(ThisDto))
                    ThisDtoHost.Role_Permissions.Remove(ThisDto);
            }
            else if (dto is EntityDefDto)
            {
                var ThisDto = dto as EntityDefDto;
                if (ThisDtoHost.RootEntityDefs.Contains(ThisDto))
                    ThisDtoHost.RootEntityDefs.Remove(ThisDto);
                else if (ThisDtoHost.EntityDefs.Contains(ThisDto))
                    ThisDtoHost.EntityDefs.Remove(ThisDto);
            }
            else if (dto is DomainDto)
            {
                var ThisDto = dto as DomainDto;
                if (ThisDtoHost.RootDomains.Contains(ThisDto))
                    ThisDtoHost.RootDomains.Remove(ThisDto);
                else if (ThisDtoHost.Domains.Contains(ThisDto))
                    ThisDtoHost.Domains.Remove(ThisDto);
            }
            else if (dto is FilterRelationDto)
            {
                var ThisDto = dto as FilterRelationDto;
                if (ThisDtoHost.RootFilterRelations.Contains(ThisDto))
                    ThisDtoHost.RootFilterRelations.Remove(ThisDto);
                else if (ThisDtoHost.FilterRelations.Contains(ThisDto))
                    ThisDtoHost.FilterRelations.Remove(ThisDto);
            }
            else if (dto is PropertyDefDto)
            {
                var ThisDto = dto as PropertyDefDto;
                if (ThisDtoHost.RootPropertyDefs.Contains(ThisDto))
                    ThisDtoHost.RootPropertyDefs.Remove(ThisDto);
                else if (ThisDtoHost.PropertyDefs.Contains(ThisDto))
                    ThisDtoHost.PropertyDefs.Remove(ThisDto);
            }
            else if (dto is User_PermissionDto)
            {
                var ThisDto = dto as User_PermissionDto;
                if (ThisDtoHost.RootUser_Permissions.Contains(ThisDto))
                    ThisDtoHost.RootUser_Permissions.Remove(ThisDto);
                else if (ThisDtoHost.User_Permissions.Contains(ThisDto))
                    ThisDtoHost.User_Permissions.Remove(ThisDto);
            }
            else if (dto is PermissionDto)
            {
                var ThisDto = dto as PermissionDto;
                if (ThisDtoHost.RootPermissions.Contains(ThisDto))
                    ThisDtoHost.RootPermissions.Remove(ThisDto);
                else if (ThisDtoHost.Permissions.Contains(ThisDto))
                    ThisDtoHost.Permissions.Remove(ThisDto);
            }
            else if (dto is RoleDto)
            {
                var ThisDto = dto as RoleDto;
                if (ThisDtoHost.RootRoles.Contains(ThisDto))
                    ThisDtoHost.RootRoles.Remove(ThisDto);
                else if (ThisDtoHost.Roles.Contains(ThisDto))
                    ThisDtoHost.Roles.Remove(ThisDto);
            }
            else if (dto is User_DeniedPermissionDto)
            {
                var ThisDto = dto as User_DeniedPermissionDto;
                if (ThisDtoHost.RootUser_DeniedPermissions.Contains(ThisDto))
                    ThisDtoHost.RootUser_DeniedPermissions.Remove(ThisDto);
                else if (ThisDtoHost.User_DeniedPermissions.Contains(ThisDto))
                    ThisDtoHost.User_DeniedPermissions.Remove(ThisDto);
            }
            else if (dto is AggregateDefDto)
            {
                var ThisDto = dto as AggregateDefDto;
                if (ThisDtoHost.RootAggregateDefs.Contains(ThisDto))
                    ThisDtoHost.RootAggregateDefs.Remove(ThisDto);
                else if (ThisDtoHost.AggregateDefs.Contains(ThisDto))
                    ThisDtoHost.AggregateDefs.Remove(ThisDto);
            }
            else if (dto is DataElementDto)
            {
                var ThisDto = dto as DataElementDto;
                if (ThisDtoHost.RootDataElements.Contains(ThisDto))
                    ThisDtoHost.RootDataElements.Remove(ThisDto);
                else if (ThisDtoHost.DataElements.Contains(ThisDto))
                    ThisDtoHost.DataElements.Remove(ThisDto);
            }
            else if (dto is DatabaseDto)
            {
                var ThisDto = dto as DatabaseDto;
                if (ThisDtoHost.RootDatabases.Contains(ThisDto))
                    ThisDtoHost.RootDatabases.Remove(ThisDto);
                else if (ThisDtoHost.Databases.Contains(ThisDto))
                    ThisDtoHost.Databases.Remove(ThisDto);
            }
            else if (dto is User_RoleDto)
            {
                var ThisDto = dto as User_RoleDto;
                if (ThisDtoHost.RootUser_Roles.Contains(ThisDto))
                    ThisDtoHost.RootUser_Roles.Remove(ThisDto);
                else if (ThisDtoHost.User_Roles.Contains(ThisDto))
                    ThisDtoHost.User_Roles.Remove(ThisDto);
            }
            else if (dto is UserDto)
            {
                var ThisDto = dto as UserDto;
                if (ThisDtoHost.RootUsers.Contains(ThisDto))
                    ThisDtoHost.RootUsers.Remove(ThisDto);
                else if (ThisDtoHost.Users.Contains(ThisDto))
                    ThisDtoHost.Users.Remove(ThisDto);
            }
		}

		#endregion

	}
}

